package com.example.controller;

import com.example.constant.Constants;
import com.example.enums.ResultStatus;
import com.example.model.*;
import com.example.service.BookService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.awt.print.Book;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 阿宾
 * Date: 2025-04-10
 * Time: 20:31
 */

@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired
    private BookService bookService;

    /**
     * 添加图书
     */
    @RequestMapping(value = "/addBook",produces = "application/json")
    public String addBook(BookInfo bookInfo){
        //1.校验参数
        if(!StringUtils.hasLength(bookInfo.getBookName())
                        ||!StringUtils.hasLength(bookInfo.getAuthor())
                        || bookInfo.getCount() == null
                        || bookInfo.getPrice() == null
                        ||!StringUtils.hasLength(bookInfo.getPublish())
                        || bookInfo.getStatus() == null){
            return "输入参数不合法";

        }
        try {
            //2.添加图书
            Integer result = bookService.insertBook(bookInfo);
            if(result > 0){
                //添加成功
                return "";
            }
        }catch (Exception e){
            System.out.println("出错");
        }
        return "添加失败";
    }
    /**
     * 查询图书列表
     */
    @RequestMapping("/getBookListByPage")
    public Result<PageResult<BookInfo>> getBookListByPage(PageRequest pageRequest, HttpSession session){

        /*
        已经通过拦截器进行配置
        //从session中获取用户信息，如果用户信息为空，则说明用户未登录
        UserInfo loginUserInfo = (UserInfo)session.getAttribute(Constants.USER_SESSION_KEY);
        if(loginUserInfo == null || loginUserInfo.getId() < 0){
            return Result.nologin();
        }*/
        //用户登录，返回图书列表
        PageResult<BookInfo> bookList = bookService.getBookListByPage(pageRequest);
        return Result.success(bookList);
    }

    /**
     * 修改图书
     * 点击修改后查询原有数据
     */
    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId){
        BookInfo bookInfo = bookService.queryBookById(bookId);
        return bookInfo;
    }

    /**
     * 修改图书
     * 修改数据后点击确定按钮实现更新数据
     * @param bookInfo
     * @return
     */
    @RequestMapping(value = "/updateBook",produces = "application/json")
    public String updateBook(BookInfo bookInfo) {
        try {
            Integer result = bookService.updateBook(bookInfo);
            if(result > 0){
                return "";
            }else {
                return "内部错误,联系管理员";
            }
        } catch (Exception e) {
            return "更新图书失败"+e.getMessage();
        }
    }

    /**
     * 删除图书
     * @param bookId
     * @return
     */
    @RequestMapping(value = "/deleteBook",produces = "application/json")
    public String deleteBook(Integer bookId) {
        try {
            Integer result = bookService.deleteBook(bookId);
            if(result > 0){
                return "";
            }else {
                return "内部错误,联系管理员";
            }
        } catch (Exception e) {
            return "删除图书失败"+e.getMessage();
        }
    }

    /**
     * 批量删除图书
     * @param bookIds
     * @return
     */
    @RequestMapping(value = "/batchDeleteBook",produces = "application/json")
    public String batchDeleteBook(@RequestParam List<Integer> bookIds){
        System.out.println();
        try {
            Integer result = bookService.batchDeleteBook(bookIds);
            if(result > 0){
                return "";
            }else {
                return "内部错误,联系管理员";
            }
        } catch (Exception e) {
            return "删除图书失败"+e.getMessage();
        }
    }
}
